﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Reader.UC
{
    /// <summary>
    /// Interaction logic for ReaderUC.xaml
    /// </summary>
    public partial class ReaderUC : UserControl
    {
        public event EventHandler OnOpenFly;

        public ReaderUC()
        {
            InitializeComponent();
        }

        private void FirstBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DocViewer.FirstPage();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! \n" + ex);
            }
        }

        private void PrevBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DocViewer.PreviousPage();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! \n" + ex);
            }
        }

        private void NextBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DocViewer.NextPage();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! \n" + ex);
            }
        }

        private void LastBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DocViewer.LastPage();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! \n" + ex);
            }
        }

        private void ZoomMinusBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DocViewer.DecreaseZoom();
                this.ZoomBtn.Content = ((int)this.DocViewer.Zoom).ToString() + " %";
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! \n" + ex);
            }
        }

        private void ZoomPlusBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DocViewer.IncreaseZoom();
                this.ZoomBtn.Content = ((int)this.DocViewer.Zoom).ToString() + " %";
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error! \n" + ex);
            }
        }

        private void ContentBtn_Click(object sender, RoutedEventArgs e)
        {
            (sender as Button).ContextMenu.IsEnabled = true;
            (sender as Button).ContextMenu.PlacementTarget = (sender as Button);
            (sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
            (sender as Button).ContextMenu.HorizontalOffset = -40;
            (sender as Button).ContextMenu.IsOpen = true;
            if (this.DocViewer.Document != null && this.DocViewer.PageCount > 0)
            {
                this.GotoTb.Text = this.DocViewer.MasterPageNumber.ToString();
            }
        }

        private void ZoomBtn_Click(object sender, RoutedEventArgs e)
        {
            (sender as Button).ContextMenu.IsEnabled = true;
            (sender as Button).ContextMenu.PlacementTarget = (sender as Button);
            (sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
            (sender as Button).ContextMenu.HorizontalOffset = -15;
            (sender as Button).ContextMenu.IsOpen = true;
        }

        private void DocViewer_LayoutUpdated(object sender, EventArgs e)
        {
            if (this.DocViewer.PageCount > 0)
            {
                this.ContentBtn.Content = this.DocViewer.MasterPageNumber.ToString() + "/" + this.DocViewer.PageCount;
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.DocViewer.PageCount > 0)
                {
                    string s = (sender as MenuItem).Header.ToString();
                    switch (s)
                    {
                        case "200 %":
                            this.DocViewer.Zoom = 200;
                            break;
                        case "160 %":
                            this.DocViewer.Zoom = 160;
                            break;
                        case "120 %":
                            this.DocViewer.Zoom = 120;
                            break;
                        case "100 %":
                            this.DocViewer.Zoom = 100;
                            break;
                        case "80 %":
                            this.DocViewer.Zoom = 80;
                            break;
                    }
                    this.ZoomBtn.Content = ((int)this.DocViewer.Zoom).ToString() + " %";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error! \n" + ex);
            }  
        }

        private void GotoBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.DocViewer.PageCount > 0)
            {
                int i;
                bool r = int.TryParse(GotoTb.Text, out i);
                if (r)
                {
                    if (i > this.DocViewer.PageCount)
                    {
                        i = this.DocViewer.PageCount;
                        GotoTb.Text = i.ToString();
                    }
                    if (i < 1)
                    {
                        i = 1;
                        GotoTb.Text = i.ToString();
                    }
                    this.DocViewer.GoToPage(i);
                }     
            }
        }

        private void BtnFlyout_Click(object sender, RoutedEventArgs e)
        {
            if (this.OnOpenFly != null)
                this.OnOpenFly(this, EventArgs.Empty);
        }
    }
}
